/*读入文件--------------------------*/

// function readFile(){
//     var file = $("#readFile")[0].files[0];
//     var reader = new FileReader();
//     reader.readAsText(file,"utf-8");
//     reader.onload = function () {
//         // console.log(this.result);
//         var content = this.result;
//         console.log(content);//文件内容
//         console.log(file.name);//文件名
//         window.localStorage.setItem("fileName",file.name);
//         var isNew = window.localStorage.getItem("isNew");
//         console.log("isNew:"+isNew);
//         window.localStorage.setItem("isNew",!isNew);
//         $("#content")[0].innerHTML = this.result.replace(/</g,'(').replace(/>/g,')');
//         $("#content2")[0].innerHTML = this.result.replace(/</g,'(').replace(/>/g,')');
//     }
// }
/*---------------------------------*/
window.onload = function(){
    var username = document.getElementById("getUsername").innerText;
    window.localStorage.setItem("username",username);
}
$("#logout").click(function () {
    window.localStorage.clear();
    window.location.href = "login.html";
})
var app = angular.module("app",[]);
// app.config(['$interpolateProvider', function($interpolateProvider) {
//   $interpolateProvider.startSymbol('{[');
//   $interpolateProvider.endSymbol(']}');
// }]);
app.controller("table",function($scope,$http){
    var urls = "http://211.83.111.224:5000/emr/annotation/";
    $("#importFile").click(function(){
        $("#readFile").click();
    })
    $scope.sel = true;
    /*初始化变量*/
    $scope.aa = false;
    $scope.categoryList = [];
    $scope.entityList = [];
    $scope.entityList2 = [];
    $scope.selectValue="";
    $scope.entityRemoved = [];
    $scope.allowEdit = false;
    $scope.categories = ["症状和体征","检查和检验","疾病和诊断","治疗","身体部位"];
    var content = document.getElementById("content");
    /*----------*/
    /*读取本地病历文本*/
    $scope.readFile = function (){
        $scope.categoryList = [];
        $scope.entityList = [];
        $scope.entityList2 = [];
        $scope.reloadStyle();
        var file = $("#readFile")[0].files[0];
        var reader = new FileReader();
        reader.readAsText(file,"utf-8");
        console.log("test:::::"+$scope.entityList);
        reader.onload = function () {

            // console.log(this.result);
            var content = this.result;
            // console.log(content);//文件内容
            // console.log(file.name);//文件名
            window.localStorage.setItem("fileName",file.name);
            $("#content")[0].value = this.result;
            $("#content2")[0].innerHTML = this.result;
        }
    }
    /*---------------------------------*/
    /*从服务器读取病历文本*/
    $scope.loadFile = function(){
        $scope.aa = true;
        $http({
            method:"POST",
            url:urls+"getContent",
            data:{fileId:document.getElementById("fileId").value}//此处应为文本id
        }).then(function successCallback(response){
            $scope.categoryList = [];
            $scope.entityList = [];
            $scope.entityList2 = [];
            $scope.allowEdit = false;
            $scope.sel = true;
            $scope.reloadStyle();
            console.log('success');
            console.log(response);
            //获取ocntent成功回调函数
            $("#content")[0].value = response.data.result.replace(/</g,'(').replace(/>/g,')');
            $("#content2")[0].innerHTML = response.data.result.replace(/</g,'(').replace(/>/g,')');
            // $scope.fileId = response.data.fileId;
            window.localStorage.setItem("fileId",response.data.fileId);
        },function errorCallback(response){
            //获取ocntent失败回调函数
            alert("失败："+response.error);
        })
    }
    /*---------------------------------*/
    /*修改实体的显示与隐藏*/
    $scope.refreshEntity = function(){
        $scope.sel = !$scope.sel;
    }
    /*---------------------------------*/
    /*从服务器下载实体*/
    $scope.loadEntity = function(){
        $scope.aa = true;
        $http({
            method:"POST",
            url:urls+"getEntity",
            data:{
                fileId:document.getElementById("fileId").value,
                editUser:window.localStorage.getItem("username")
            }//此处应为文本id
        }).then(function successCallback(response){
            console.log("下载实体已执行");
            console.log(response);
            $scope.entityList = [];
            $scope.entityList2 = [];
            if(response.data.flag == 1){
                $scope.sel = false;
                console.log(response.data.entityList.length);
                for(i=0;i<response.data.entityList.length;i++){
                    $scope.entityList[i] = response.data.entityList[i];
                }
                // $scope.$watch('$viewContentLoaded', function() {
                // var sels = document.getElementsByTagName('select');
                // for(i=0;i<sels.length;i++){
                //     console.log("for循环开始执行");
                //     $scope.sel = false;
                //     // sels[i].removeAttribute("ng-model");
                //     // sels[i].setAttribute("value","治疗")
                // }
                // });
                $scope.allowEdit = true;
                $scope.reloadStyle();
            }
        },function errorCallback(response){
            //获取ocntent失败回调函数
            alert("失败："+response.error);
        })
    }
    /*---------------------------------*/
    /*为实体添加点击，添加颜色*/
    $scope.addClickEvent = function(){
        var originContent = content.value;
        document.getElementById("content3").innerHTML = originContent;
        var items = document.getElementsByClassName("item");
        $scope.$watch('$viewContentLoaded', function() {
            console.log("items:")
            console.log(items);
            console.log(items.length);
            for(i=0;i<items.length;i++){
                items[i].addEventListener("click",function(){
                    // console.log(this.getElementsByClassName("pos_b")[0].innerHTML);
                    // console.log(this.getElementsByClassName("pos_e")[0].innerHTML);
                    var startPos = this.getElementsByClassName("pos_b")[0].innerHTML;
                    var endPos = this.getElementsByClassName("pos_e")[0].innerHTML;
                    var thisCategory = this.getElementsByClassName("category")[0].innerHTML;
                    var divContent = document.getElementById("content").value;
                    var entityName = divContent.slice(startPos,endPos);
                    divContent = divContent.split('');
                    divContent.splice(endPos,0,"</span>");
                    if(thisCategory == "症状和体征"){
                        divContent.splice(startPos,0,"<span class='class1'>");
                    }else if(thisCategory == "检查和检验"){
                        divContent.splice(startPos,0,"<span class='class2'>");
                    }else if(thisCategory == "疾病和诊断"){
                        divContent.splice(startPos,0,"<span class='class3'>");
                    }else if(thisCategory == "治疗"){
                        divContent.splice(startPos,0,"<span class='class4'>");
                    }else if(thisCategory == "身体部位"){
                        divContent.splice(startPos,0,"<span class='class5'>");
                    }
                    // divContent.splice(startPos,1,"<span class='class1'>");
                    // console.log("2--"+divContent);
                    divContent.join('')
                    // var newTxt = divContent.replace(divContent.slice(startPos,endPos),"<span class='class1'>"+divContent.slice(startPos,endPos)+"</span>");
                    document.getElementById("content3").innerHTML = divContent.join('');
                })
            }
        })
    }
    /*---------------------------------*/
    /*从服务器下载全部用户添加的实体*/
    $scope.reviewEntity = function(){
        $http({
            method:"POST",
            url:urls+"reviewEntity",
            data:{
                fileId:document.getElementById("fileId").value
                /**/
            }
        }).then(function successCallback(response){
            if(response.data.flag == 1){
                // $scope.aa = true;
                // $scope.allowEdit = true;
                $scope.allowEdit = true;
                $scope.aa = false;
                $scope.sel = false;
                console.log($scope.entityList)
                for(i=0;i<response.data.entityList.length;i++){
                    $scope.entityList[i] = response.data.entityList[i];
                }
                console.log($scope.entityList);
                // var res = [$scope.entityList[0]];
                // for(var i=1;i<$scope.entityList.length;i++){
                //     var repeat = false;
                //     var nums = 0;
                //     for(var j=0;j<res.length;j++){
                //         if($scope.entityList[i].startPosition == res[j].startPosition && $scope.entityList[i].endPosition ==res[j].endPosition){
                //             repeat = true;
                //             nums ++ ;
                //         }
                //     }
                //     if(!repeat){
                //         res.push($scope.entityList[i]);
                //     }
                // }
                // console.log(res)
                $scope.addClickEvent();
                for(i=0;i<$scope.indexList.length;i++){
                    $scope.entityList2.length = $scope.entityList.length;
                    $scope.entityList2[$scope.indexList.length-1-i] = $scope.entityList[$scope.indexList[i]];
                    var startPos = $scope.entityList[$scope.indexList[i]].startPosition;
                    var endPos = $scope.entityList[$scope.indexList[i]].endPosition;
                    var thisCategory = $scope.entityList[$scope.indexList[i]].category;
                    var divContent = document.getElementById("content2").innerHTML;
                    var entityName = divContent.slice(startPos,endPos);
                    divContent = divContent.split('');
                    divContent.splice(endPos,0,"</span>");
                    if(thisCategory == "症状和体征"){
                        divContent.splice(startPos,0,"<span class='class1'>");
                    }else if(thisCategory == "检查和检验"){
                        divContent.splice(startPos,0,"<span class='class2'>");
                    }else if(thisCategory == "疾病和诊断"){
                        divContent.splice(startPos,0,"<span class='class3'>");
                    }else if(thisCategory == "治疗"){
                        divContent.splice(startPos,0,"<span class='class4'>");
                    }else if(thisCategory == "身体部位"){
                        divContent.splice(startPos,0,"<span class='class5'>");
                    }
                    // divContent.splice(startPos,1,"<span class='class1'>");
                    // console.log("2--"+divContent);
                    divContent.join('')
                    // var newTxt = divContent.replace(divContent.slice(startPos,endPos),"<span class='class1'>"+divContent.slice(startPos,endPos)+"</span>");
                    document.getElementById("content2").innerHTML = divContent.join('');
                }
            }
            /**/
        },function errorCallback(response){
            /**/
        })
    }
    /*---------------------------------*/
    /*上传最终的实体到数据库*/
    $scope.pushChecked = function(){
        console.log($scope.entityList);
        var jsons = {};
        jsons.entities = [];
        for(var j =0;j<$scope.entityList.length;j++){
            if($scope.entityList[j].category){
                jsons.entities.push({
                    "name":$scope.entityList[j].name,
                    "startPosition":$scope.entityList[j].startPosition.toString(),
                    "endPosition":$scope.entityList[j].endPosition.toString(),
                    "category":$scope.entityList[j].category
                })
                jsons.fileId = window.localStorage.getItem("fileId");
                jsons.editUser = "admin";
                jsons.editTime = new Date().getTime();
                jsons.entityRemove = $scope.entityRemoved;
                // jsons.fileName = window.localStorage.getItem("fileName");
                // console.log(jsons);
            }else{
                alert("请选择类别");
                break;
            }
        }
        if($scope.entityList.length>0){
            $http({
                method:"post",
                url:urls+"edit",
                data:JSON.stringify(jsons)
            }).then(function successCallback(response){
                if(response.data.flag == 1){
                    alert("上传完成！")
                }
            },function errorCallback(response){
                alert("失败："+response.statusText);
            })
        }
        else{
            alert('尚未添加任何实体！')
        }
    }
    /*----------------------------------*/
    /*重新加载content2中的颜色样式*/
    $scope.reloadStyle = function(){
        function NumDescSort(a,b)
        {
            return b - a;
        }
        var arr = [];
        var originArr = [];
        $scope.indexList = [];
        // arr.sort(NumDescSort);
        for(i=0;i<$scope.entityList.length;i++){
            arr.push($scope.entityList[i].startPosition);
        }
        var originArr = arr.slice();
        arr.sort(NumDescSort);//倒序
        for(i=0;i<arr.length;i++){
            $scope.indexList.push(originArr.indexOf(arr[i]));
        }
        var originContent = content.value;
        document.getElementById("content2").innerHTML = originContent;
        for(i=0;i<$scope.indexList.length;i++){
            $scope.entityList2.length = $scope.entityList.length;
            $scope.entityList2[$scope.indexList.length-1-i] = $scope.entityList[$scope.indexList[i]];
            var startPos = $scope.entityList[$scope.indexList[i]].startPosition;
            var endPos = $scope.entityList[$scope.indexList[i]].endPosition;
            var thisCategory = $scope.entityList[$scope.indexList[i]].category;
            var divContent = document.getElementById("content2").innerHTML;
            var entityName = divContent.slice(startPos,endPos);
            // console.log(entityName);
            divContent = divContent.split('');
            // console.log("1--"+divContent);
            // divContent.splice(startPos,endPos,"<span class='class1'>"+entityName+"</span>");
            divContent.splice(endPos,0,"</span>");
            if(thisCategory == "症状和体征"){
                divContent.splice(startPos,0,"<span class='class1'>");
            }else if(thisCategory == "检查和检验"){
                divContent.splice(startPos,0,"<span class='class2'>");
            }else if(thisCategory == "疾病和诊断"){
                divContent.splice(startPos,0,"<span class='class3'>");
            }else if(thisCategory == "治疗"){
                divContent.splice(startPos,0,"<span class='class4'>");
            }else if(thisCategory == "身体部位"){
                divContent.splice(startPos,0,"<span class='class5'>");
            }
            // divContent.splice(startPos,1,"<span class='class1'>");
            // console.log("2--"+divContent);
            divContent.join('')
            // var newTxt = divContent.replace(divContent.slice(startPos,endPos),"<span class='class1'>"+divContent.slice(startPos,endPos)+"</span>");
            document.getElementById("content2").innerHTML = divContent.join('');
        }
    }
    /*----------------------------------*/
    /*监听select选项框中的option改变*/
    $scope.change = function(){
        var index = this.$index;
        var selectedValue = this.selectValue;
        $scope.categoryList[index] = selectedValue;
        // console.log("类别列表："+$scope.categoryList);
        for(i=0;i<$scope.entityList.length;i++){
            //将选择的类别添加为实体对象属性
            $scope.entityList[index].category = $scope.categoryList[index];
        }
        console.log($scope.entityList)
        $scope.reloadStyle();
    }
    /*----------------------------------*/
    /*添加实体-------------------------*/
    $scope.addEntity = function(){
        $scope.text = "";
        if(navigator.appName=="Microsoft Internet Explorer"){
            alert("不支持IE浏览器");
        }else{
            var sel = window.getSelection();
            // console.log("开始位置"+document.getElementById("content").selectionStart);
            // console.log("结束位置"+document.getElementById("content").selectionEnd);
            $scope.text=window.getSelection().toString();
        }
        if($scope.text!=""){
            // $scope.wordList.push($scope.text);
            $scope.entity = {name:$scope.text,startPosition:content.selectionStart,endPosition:content.selectionEnd};
            $scope.entityList.push($scope.entity);
            if(!$scope.aa){
                $scope.addClickEvent();
            }
            // console.log($scope.entityList);
        }
    };
    /*---------------------------------*/
    /*删除实体------------------------*/
    $scope.removeEntity = function(){
        console.log("delete:");
        console.log(this.x.name);
        var index = this.$index;
        var r = confirm('确认删除该实体？');
        if(r){
            $scope.entityList.splice(index,1);
            $scope.entityRemoved.push({
                name:this.x.name,
                startPosition:this.x.startPosition
            })
            $scope.reloadStyle();
        }
        console.log($scope.entityRemoved);
    };
    /*---------------------------------*/
    /*--输出实体列表到本地-------------------*/
    $scope.exportEntity = function(){
        // console.log("实体列表");
        // console.log($scope.entityList2.length);
        var title = prompt("请输入文件名","");
        var textExport = "";
        for(var i=0;i<$scope.entityList2.length;i++){
            textExport += $scope.entityList2[i].name+"\t"+$scope.entityList2[i].startPosition+"\t"+$scope.entityList2[i].endPosition+"\t"+$scope.entityList2[i].category+"\r\n";
        }
        var blob = new Blob([textExport], {type: "text/plain;charset=utf-8"});
        saveAs(blob, title+".txt");
    }
    /*---------------------------------*/
    /*---向服务器发送标注结果-----------*/
    $scope.pushToDB = function(){
        var jsons = {};
        jsons.entities = [];
        for(var j =0;j<$scope.entityList2.length;j++){
            if($scope.entityList2[j].category){
                jsons.entities.push({
                    "name":$scope.entityList2[j].name,
                    "startPosition":$scope.entityList2[j].startPosition.toString(),
                    "endPosition":$scope.entityList2[j].endPosition.toString(),
                    "category":$scope.entityList2[j].category
                })
                jsons.fileId = window.localStorage.getItem("fileId");
                jsons.editUser = window.localStorage.getItem("username");
                jsons.editTime = new Date().getTime();
                jsons.entityRemove = $scope.entityRemoved;
                // jsons.fileName = window.localStorage.getItem("fileName");
                // console.log(jsons);
            }else{
                alert("请选择类别");
                break;
            }
        }
        if($scope.entityList.length>0){

            $http({
                method:"post",
                url:urls+"edit",
                data:JSON.stringify(jsons)
            }).then(function successCallback(response){
                if(response.data.flag == 1){
                    alert("上传完成！")
                }
            },function errorCallback(response){
                alert("失败："+response.statusText);
            })
        }
        else{
            alert('尚未添加任何实体！')
        }
    }
    /*---------------------------------*/
})